package com.yunmaozj.tools.lock.annotations;

import com.yunmaozj.tools.lock.RedisLock;
import com.yunmaozj.tools.lock.interceptor.DistributedLockAttributeSourceAdvisor;
import com.yunmaozj.tools.lock.interceptor.DistributedLockInterceptor;
import org.springframework.context.annotation.*;

import static com.yunmaozj.tools.lock.RedisLock.LockType.DEFAULT;
import static org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE;

/**
 * @author 钟明宏
 * @version V1.0
 * @date 2021-08-29 20:43
 **/
@Configuration
public class DistributedLockConfiguration extends AbstractDistributedLockConfiguration {

    @Bean
    @Role(ROLE_INFRASTRUCTURE)
    public DistributedLockAttributeSourceAdvisor DistributedLockAttributeSourceAdvisor( DistributedLockInterceptor DistributedLockInterceptor) {
        AnnotationDistributedLockAttributeSource DistributedLockAttributeSource = new AnnotationDistributedLockAttributeSource(new DistributedLockAnnotationParser());
        DistributedLockInterceptor.setDistributedLockAttributeSource(DistributedLockAttributeSource);

        DistributedLockAttributeSourceAdvisor attributeSourceAdvisor = new DistributedLockAttributeSourceAdvisor(DistributedLockAttributeSource);
        attributeSourceAdvisor.setAdvice(DistributedLockInterceptor);
        if (enableDL != null) {
            attributeSourceAdvisor.setOrder(enableDL.getNumber("order"));
        }
        return attributeSourceAdvisor;
    }

    @Bean
    public RedisLock redisLock(){
        RedisLock redisLock = new RedisLock("127.0.0.1", DEFAULT);
        redisLock.afterProperties();
        return redisLock;
    }

    @Bean
    public DistributedLockInterceptor DistributedLockInterceptor(){
        DistributedLockInterceptor DistributedLockInterceptor = new DistributedLockInterceptor();
        DistributedLockInterceptor.setDistributedLockAttributeSource(null);
        DistributedLockInterceptor.setRedisLock(null);
        return DistributedLockInterceptor;
    }
}
